//完全体
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <windows.h>

#define high 20//画面尺寸 
#define width 50
#define enemynumber 5//敌机数量
//全局变量

int canvas[high][width]={0};//对应元素
int posx,posy;//飞机坐标 
int enemyx[enemynumber],enemyy[enemynumber];//敌机坐标
int score;//得分 
int bulletwidth;//子弹宽度
int count;//速度计数

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x,int y)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}

void startup()
{
	HideCursor(); 
	
	posx=high/2;
	posy=width/2; 
	canvas[posx][posy]=1;
	
	int k;
	for(k=0;k<enemynumber;k++)
	{
	enemyx[k]=0;
	enemyy[k]=rand()%(width-1);
	canvas[enemyx[k]][enemyy[k]]=3;
	}
	score=0;
	count=25;
	bulletwidth=0;
}

void show()
{
	gotoxy(0,0);
	
		int i,j;
	for(i=0;i<=high-1;i++)
	{
		for(j=0;j<=width-1;j++)
		{
			if(canvas[i][j]==1) 
			{
				printf("*");//输出飞机 
			}
			else if(canvas[i][j]==2)
			{
				printf("|");
			}
			else if(canvas[i][j]==3)
			{
				printf("$");
			}
			else if(j==width-1)
			{
				printf("o");
			}
			else if(i==high-1)
			{
				printf("o");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	 } 
	printf("得分：%d\n",score);
}

void updatewithinput()
{
	
	char input;
	if(kbhit())
	{
		input=getch();
		
		if(input=='a')
		{
			canvas[posx][posy]=0;
			posy--;
			canvas[posx][posy]=1;
		}
		if(input=='d')
		{
			canvas[posx][posy]=0;
			posy++;
			canvas[posx][posy]=1;
		}
		if(input=='w')
		{
			canvas[posx][posy]=0;
			posx--;
			canvas[posx][posy]=1;
		}
		if(input=='s')
		{
			canvas[posx][posy]=0;
			posx++;
			canvas[posx][posy]=1;
		}
		if(input==' ')
		{
			int x;
			int left,right;
			left=posy-bulletwidth;
			if(left<0)
			{
				left=0;
			}
			right=posy+bulletwidth;
			if(right>width-2)
			{
				right=width-2;
			}
			for(x=left;x<=right;x++)
			{
				canvas[posx-1][x]=2;
			}	
		}
		
		if(input==27)
		{
			system("pause");
		}	
	}	
}

void updatewithoutinput()
{
	int i,j,k;
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(canvas[i][j]==2)
			{
				
			for(k=0;k<enemynumber;k++)
			{		
				if(i==enemyx[k]&&j==enemyy[k])//击中敌机 
				{
					enemyx[k]=0;
					enemyy[k]=rand()%(width-1);
					canvas[enemyx[k]][enemyy[k]]=3;
					score++;
					if(score%5==0&&count>5)
					{
						count--;
					}
					if(score%10==0&&bulletwidth<7)
					{
						bulletwidth++;
					}


				}
			}

				canvas[i][j]=0;
				if(i>0)
				{
					canvas[i-1][j]=2;//子弹自动上升 
				}
			}
		}
	}
	for(k=0;k<enemynumber;k++)
	{
		if(enemyx[k]>high)
		{
		enemyx[k]=0;
		enemyy[k]=rand()%(width-1);
		canvas[enemyx[k]][enemyy[k]]=3;//重生敌机
		score--;
		}
	}

	static int speed=0;

	if(speed<count)//加速
	{
		speed++;
	}
	if(speed==count)
	{
		for(k=0;k<enemynumber;k++)
		{
			canvas[enemyx[k]][enemyy[k]]=0;
			enemyx[k]++;
			canvas[enemyx[k]][enemyy[k]]=3;
			speed=0;
		}
	}

	if(posx==high-1)//防止出界
	{
		canvas[posx][posy]=0;
		posx--;
		canvas[posx][posy]=1;
	}
	if(posx==0)//防止出界
	{
		canvas[posx][posy]=0;
		posx++;
		canvas[posx][posy]=1;
	}
	if(posy==width-1)
	{
		canvas[posx][posy]=0;
		posy--;
		canvas[posx][posy]=1;
	}
	if(posy==0)
	{
		canvas[posx][posy]=0;
		posy++;
		canvas[posx][posy]=1;
	}

for(k=0;k<enemynumber;k++)
{
	if(posx==enemyx[k]&&posy==enemyy[k])
	{
		printf("game over!!!");
		exit(0);
	}
}


}

int main()
{
	startup();
	
	while(1)
	{
		show();
		updatewithinput();
		updatewithoutinput();
		
	}	
	return 0;
 } 